package com.yhp.service.iml;

import com.yhp.bean.Menu;
import com.yhp.bean.Role;
import com.yhp.bean.Users;
import com.yhp.dao.RoleDao;
import com.yhp.dao.UsersDao;
import com.yhp.dao.iml.RoleDaoImpl;
import com.yhp.dao.iml.UsersDaoImpl;
import com.yhp.service.UsersService;

import java.util.ArrayList;
import java.util.List;

public class UsersServiceImpl implements UsersService {
    private UsersDao usersDao = new UsersDaoImpl();
    private RoleDao roleDao = new RoleDaoImpl();
    @Override
    public Users getUsers(String usersName, String passWord) {
        Users users = usersDao.getUsers(usersName, passWord);
        if (users==null){
            return null;
        }
        //根据角色id查询角色，菜单信息（三表联查）
        Integer roleId = users.getRoleId();
        Role role = roleDao.findRoleById(roleId);
        //对菜单进行分级
        List<Menu> menuList = role.getMenuList();
        List<Menu> newMenuList = new ArrayList<>();//存储分一二的菜单
        for (Menu menu : menuList) {
            if (menu.getUpMenuId()==0){//说明是一级菜单
                List<Menu> secondMenuList = new ArrayList<>();
                for (Menu second : menuList) {
                    if (second.getUpMenuId()==menu.getMenuId()){
                        secondMenuList.add(second);
                    }
                }
                menu.setSecondMenuList(secondMenuList);
                newMenuList.add(menu);
            }
            role.setMenuList(newMenuList);
            users.setRole(role);
        }
        return users;
    }

    @Override
    public List<Users> getUsersList(int pageIndex, int pageSize) {
        return usersDao.getUsersList(pageIndex,pageSize);
    }

    @Override
    public int total() {
        return usersDao.total();
    }

    @Override
    public int addUser(Users users) {
        return usersDao.addUser(users);
    }

    @Override
    public Users findById(int userId) {
        return usersDao.findById(userId);
    }

    @Override
    public int updateUser(Users users) {
        return usersDao.updateUser(users);
    }

    @Override
    public int deleteUserById(int userId) {
        return usersDao.deleteUserById(userId);
    }
}
